//
//  touzifangshenViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/10.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class touzifangshenViewController: UIViewController {
    var tableView  : UITableView!
    
    var shengArray = [rouzifangModel]()
    var hezuofangArray = [hezuofangModel]()
    var hezuofang:String!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = bgGrayColor;
        
        if self.hezuofang == "合作方"{
             navigationItem.title="单位地址"
        }else{
             navigationItem.title="选择省"
        }
        
       
        tableView = UITableView(frame:self.view.frame, style:.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        
        tableView.tableFooterView = UIView.init()
 
        
        self.view.addSubview(tableView)
        tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        
        self.loadData()
    }
    
    func loadData(){
        
        if self.hezuofang == "合作方"{
            let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/datadictionary/list"
            
            let json = "{\"parentCode\":\"\("unitAddress")\"}"
            
            //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
            print("json====\(json)")
            
            let url = URL(string: urlString)!
            let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
            
            var request = URLRequest(url: url)
            request.httpMethod = HTTPMethod.post.rawValue
            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = jsonData
            
            Alamofire.request(request).responseJSON {(response) in
                
                print(response)
                if response.result.isSuccess {
                    let json = JSON(response.data!)
                    let array = json["resultData"].array
                    
                    for dataDic in  array! {
                        let model1 =   hezuofangModel()
                        
                        
                        model1.dictionaryName = dataDic["dictionaryName"].string
                        
                        model1.dictionaryValue = dataDic["dictionaryValue"].string
                        
                        
                        self.hezuofangArray.append(model1)
                    }
                    self.tableView.reloadData()
                    
                }else{
                    
                }
                
            }
  
            
        }
        else{
            let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/province/list"
            
            
            
            let url = URL(string: urlString)!
            
            
            var request = URLRequest(url: url)
            request.httpMethod = HTTPMethod.post.rawValue
            
            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            Alamofire.request(request).responseJSON {(response) in
                
                
                
                print(response)
                if response.result.isSuccess {
                    let json = JSON(response.data!)
                    let array = json["resultData"].array
                    
                    for dataDic in  array! {
                        
                        let model1 =   rouzifangModel()
                        
                        
                        model1.ids = dataDic["id"].intValue
                        
                        model1.provinceName = dataDic["provinceName"].string
                        
                        
                        self.shengArray.append(model1)
                        
                        
                        
                        
                    }
                    
                    
                    
                    self.tableView.reloadData()
                    
                    
                }else{
                    
                }
                
            }
        }
        
        
        
        
 
    }


}
extension touzifangshenViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.hezuofang == "合作方" {
             return self.hezuofangArray.count
        }else{
             return self.shengArray.count
        }
       
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 40
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        if self.hezuofang == "合作方" {
            let hezuofang:hezuofangModel = self.hezuofangArray[indexPath.row]
            
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            if cell == nil {
                
                cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
            }
            cell?.textLabel?.text = hezuofang.dictionaryName
            cell?.textLabel?.font = UIFont.systemFont(ofSize:20)
            
            return cell!
        }else{
            let nextListModel:rouzifangModel = self.shengArray[indexPath.row]
            
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            if cell == nil {
                
                cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
            }
            cell?.textLabel?.text = nextListModel.provinceName
            cell?.textLabel?.font = UIFont.systemFont(ofSize:20)
            
            return cell!
        }
        
       
        
      
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if self.hezuofang == "合作方" {
            let dynamicModel:hezuofangModel = self.hezuofangArray[indexPath.row]
            NotificationCenter.default.post(name: NSNotification.Name("hezuofang"), object: self, userInfo: ["shenID":dynamicModel.dictionaryName,"shenName":dynamicModel.dictionaryValue])

           self.navigationController?.popViewController(animated: true)
            
            
        }else{
            let dynamicModel:rouzifangModel = self.shengArray[indexPath.row]
            
            
            let WebVC =  touzifangshiViewController()
            WebVC.shiID = String(dynamicModel.ids)
            
            WebVC.shiName = dynamicModel.provinceName
           
            self.navigationController?.pushViewController(WebVC , animated: true)
        }
        
        
       
        
    }
    
    
}
